package com.apestech.framework.util;

import java.util.Calendar;

public class UnitUtil {
	
	public static String getCostTime(long ms){
		Calendar c=Calendar.getInstance();
		c.setTimeInMillis(ms);
		int minute=c.get(Calendar.MINUTE);
		int second=c.get(Calendar.SECOND);
		int millisecond=c.get(Calendar.MILLISECOND);
		if(minute>0){
			return minute+"分"+second+"秒"+millisecond+"微秒";
		} else if(second>0){
			return second+"秒"+millisecond+"微秒";
		} else if(millisecond>0){
			return millisecond+"微秒";
		}
		return "";
	}
	
    public static double getMillisecond(long ms){
    	return MathUtil.round(ms/1000, 2);
    }
    
    public static double getSecond(double d){
    	return MathUtil.round(d / 60, 2);
    }
    
    public static String getMinutes(long ms){
    	double d=ms;
    	String lx="豪秒";
    	if(ms>1000){
    		d=getMillisecond(ms);
    		lx="秒";
    		if(d>60){
    			d=getSecond(d);
    			lx="分";
    			if(d>60){
        			d=getSecond(d);
        			lx="小时";
        		}
    		}
    	}
    	return FormatUtil.formatNumberFloor(d, 2)+lx;
    }
    
    
    public static String getSize(long b) throws IllegalAccessException{ 
		double d=getKB(b);
		String str="kb";
		if(d>=1024){
			d=getMB(d);
			str="mb";
			if(d>1024){
				d=getGB(d);
				str="bb";
				if(d>1024){
					d=getTB(d);
					str="tb";
				}
			}
		}
		return FormatUtil.formatNumberFloor(d, 2)+str;
	}
	
	public static double getKB(long b) throws IllegalAccessException{
		return MathUtil.floor(MathUtil.div(b, 1024.00), 2);
	}
	
	public static double getMB(double b) throws IllegalAccessException{
		return MathUtil.floor(MathUtil.div(b, 1024.00), 2);
	}
	
	public static double getMB(long b) throws IllegalAccessException{
		return MathUtil.floor(MathUtil.div(getKB(b), 1024.00), 2);
	}
	
	public static double getGB(double b) throws IllegalAccessException{
		return MathUtil.floor(MathUtil.div(getMB(b), 1024.00), 2);
	}
	
	public static double getGB(long b) throws IllegalAccessException{
		return MathUtil.floor(MathUtil.div(getMB(b), 1024.00), 2);
	}
	
	public static double getTB(double b) throws IllegalAccessException{
		return MathUtil.floor(MathUtil.div(getTB(b), 1024.00), 2);
	}
	
	private static double getTB(long b) throws IllegalAccessException{
		return MathUtil.floor(MathUtil.div(getTB(b), 1024.00), 2);
	}
	
	public static long KB2B(double d){
		return (long) MathUtil.floor(d*1024, 0); 
	}
	
	public static long MB2B(double d){
		return (long) MathUtil.floor(KB2B(d)*1024, 0);
	}
	
	public static long GB2B(double d){
		return (long) MathUtil.floor(MB2B(d)*1024, 0);
	}
	
	public static long TB2B(double d){
		return (long) MathUtil.floor(GB2B(d)*1024, 0);
	}

}
